package com.taskcollection.control;

import com.alibaba.fastjson.JSONObject;
import com.taskcollection.bean.StudentBean;
import com.taskcollection.bean.TeacherBean;
import com.taskcollection.service.StudentService;
import com.taskcollection.service.TeacherService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Administrator on 2016/8/6 0006.
 */
public class RegControl {
    public void reg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取注册信息
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String passwd = request.getParameter("passwd");
        String status = request.getParameter("status");
        System.out.println(status+"***********************");
        if("教师".equals(status)){
            //声明teacherservice对象
            TeacherService ts = new TeacherService();
            //声明teacherbean对象
            TeacherBean tea = new TeacherBean();
            tea.setTeacher_name(name);
            tea.setTeacher_phone(phone);
            tea.setTeacher_passwd(passwd);
            boolean flag = ts.reg(tea);
            if(!flag){
                response.sendRedirect(request.getContextPath()+"/teacher/homepage.jsp");
            }
        }else if("学生".equals(status)){
            System.out.println(status+"进入RegServlet");
            StudentService ss = new StudentService();
            StudentBean stu = new StudentBean();
            stu.setStudent_name(name);
            stu.setStudent_phone(phone);
            stu.setStudent_passwd(passwd);
            boolean flag = ss.reg(stu);
            if(!flag){
                response.sendRedirect(request.getContextPath()+"/teacher/homepage.jsp");
            }
        }

    }
    public void checkPhone(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String phone = request.getParameter("phone");
        TeacherService ts = new TeacherService();
        StudentService ss = new StudentService();
        boolean isUseT = ts.checkPhone(phone);
        boolean isUseS = ss.checkPhone(phone);
        JSONObject json = new JSONObject();
        if(isUseS||isUseT){
            json.put("msg","used");
        }else {
            json.put("msg","unusd");
        }
        response.getWriter().print(json.toJSONString());
    }
}
